/**
 * $Id$
 * @author mcanovas
 * @date   Feb 29, 2012 12:41:43 PM
 *
 * Copyright (C) 2012 Scytl Secure Electronic Voting SA
 *
 * All rights reserved.
 *
 */
package org.sk.gsync.app.spring;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.sk.gsync.app.mybatis.DatabaseGenerator;
import org.sk.gsync.app.mybatis.FolderMapper;
import org.sk.gsync.app.mybatis.QueueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
public class MyBatisSpringConfig {

    @Autowired
    @Qualifier("datasource")
    private DataSource dataSource;

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactoryBean getSqlSessionFactoryBean() {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean;
    }

    @Bean(name = "transactionManager")
    public DataSourceTransactionManager getTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "folderMapper")
    public MapperFactoryBean<FolderMapper> getFolderMapper() {
        MapperFactoryBean<FolderMapper> mapperFactoryBean =
            new MapperFactoryBean<FolderMapper>();
        mapperFactoryBean.setMapperInterface(FolderMapper.class);
        return mapperFactoryBean;
    }

    @Bean(name = "queueMapper")
    public MapperFactoryBean<QueueMapper> getQueueMapper() {
        MapperFactoryBean<QueueMapper> mapperFactoryBean =
            new MapperFactoryBean<QueueMapper>();
        mapperFactoryBean.setMapperInterface(QueueMapper.class);
        return mapperFactoryBean;

    }

    @Bean(name = "databaseGenerator", initMethod = "generate")
    public DatabaseGenerator getDatabaseGenerator() {
        return new DatabaseGenerator();
    }

}
